/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.cw.view;

import java.io.IOException;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.StringItem;
import org.cw.bean.Establishment;
import org.cw.bo.EstablishmentBO;

/**
 *
 * @author SVN - Team
 */
public class FromListEstablishments extends Form implements CommandListener {

    private static final String TITLE = "Establishments  ";
    private EstablishmentBO estBO = EstablishmentBO.getInstance();
    private MReviewer curDisplay;
    private ChoiceGroup listEsts;
    private Command cmdBack, cmdNewEst, cmdNewReview,
            cmdSearch, cmdView, cmdSearchByType,
            cmdSortDESC, cmdSortASC;
    private FromAddEstablistment frmAddEst;
    private FromAddEditReview addReview;
    private FromViewEstablistment frmViewEst;
    private FromSearchName frmSearchEst;
    private FromSearchType frmSeachByType;
    private Image iconList;
    private StringItem lbTitleList;
    private Establishment est;

    public FromListEstablishments(MReviewer curDisplay) {
        super(TITLE);
        this.curDisplay = curDisplay;
        init();
    }

    private void init() {
        
        try {
            iconList = Image.createImage("/list.png");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        append(iconList);
        lbTitleList = new StringItem("", "List Establishments");
        lbTitleList.setLayout(StringItem.LAYOUT_2);
        append(lbTitleList);
        lbTitleList.setFont(Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
        listEsts = new ChoiceGroup("", Choice.EXCLUSIVE);
        cmdBack = new Command("Back", Command.BACK, 0);
        cmdNewEst = new Command("New", Command.OK, 1);
        cmdView = new Command("View", Command.OK, 2);
        cmdNewReview = new Command("Add Reviews", Command.OK, 3);
        cmdSearch = new Command("Search", Command.OK, 4);
        cmdSearchByType = new Command("Search By Type", Command.OK, 5);
        cmdSortASC = new Command("Sort A-Z", Command.OK, 6);
        cmdSortDESC = new Command("Sort Z-A", Command.OK, 7);
        append(listEsts);
        setDataSource();
        addCommand(cmdBack);
        addCommand(cmdNewEst);
        if (MReviewer.dataStore.size() >= 1) {
            addCommand(cmdView);
            addCommand(cmdNewReview);
            addCommand(cmdSearch);
            addCommand(cmdSearchByType);
            addCommand(cmdSortDESC);
            addCommand(cmdSortASC);
        } else {
            listEsts.setLabel("Do not have records.");
        }
        setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == cmdBack) {
            curDisplay.destroyApp(true);
        } else if (c == cmdNewEst) {
            frmAddEst = new FromAddEstablistment(curDisplay);
            Display.getDisplay(curDisplay).setCurrent(frmAddEst);
        } else if (c == cmdView) {
            String nEst = listEsts.getString(listEsts.getSelectedIndex());
            est = estBO.getEstablishmentByName(nEst);
            if (est != null) {
                frmViewEst = new FromViewEstablistment(curDisplay);
                frmViewEst.setEstablishment(est);
                Display.getDisplay(curDisplay).setCurrent(frmViewEst);
            }
        } else if (c == cmdSearch) {
            frmSearchEst = new FromSearchName(curDisplay);
            Display.getDisplay(curDisplay).setCurrent(frmSearchEst);
        } else if (c == cmdSearchByType) {
            frmSeachByType = new FromSearchType(curDisplay);
            Display.getDisplay(curDisplay).setCurrent(frmSeachByType);
        } else if (c == cmdSortASC) {
            MReviewer.dataStore = estBO.sort(MReviewer.dataStore, EstablishmentBO.DESC);
            setDataSource();
        } else if (c == cmdSortDESC) {
            MReviewer.dataStore = estBO.sort(MReviewer.dataStore, EstablishmentBO.ASC);
            setDataSource();
        } else if (c == cmdNewReview) {
            String estName = listEsts.getString(listEsts.getSelectedIndex());
            est = estBO.getEstablishmentByName(estName);
            if (est != null) {
                addReview = new FromAddEditReview(curDisplay);
                addReview.setEst(est, FromAddEditReview.ADD);
                Display.getDisplay(curDisplay).setCurrent(addReview);
            }
        }
    }

    private void setDataSource() {
        listEsts.deleteAll();
        for (int i = 0; i < MReviewer.dataStore.size(); i++) {
            Establishment tmpEst = (Establishment) MReviewer.dataStore.elementAt(i);
            String strContext = "\n\t" + tmpEst.getLocation();
            strContext = "";
            try {
                if (tmpEst.getType().equals("Café")) {
                    listEsts.append(tmpEst.getName() + strContext, Image.createImage("/cafe_coffee.png"));
                } else if (tmpEst.getType().equals("Bar")) {
                    listEsts.append(tmpEst.getName() + strContext, Image.createImage("/drink_bar_cocktails.png"));
                } else if (tmpEst.getType().equals("Restaurant")) {
                    listEsts.append(tmpEst.getName() + strContext, Image.createImage("/restaurant.png"));
                }
            } catch (IOException ex) {
                listEsts.append(tmpEst.getName() + strContext, null);
            }
            listEsts.setFont(i, Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
        }
    }
}
